package db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import db.Controller;
import db.dto.UserDTO;

public class UserDAO {
	public UserDTO getUser(String username) {
		UserDTO user = null;
		Connection con = Controller.getConnection();
		String query = "select * from m_user where m_username = ?";
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setString(1, username);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()) {
				user = new UserDTO();
				user.setId(rs.getInt("m_id"));
				user.setUsername(rs.getString("m_username"));
				user.setPassword(rs.getString("m_password"));
				user.setRole(rs.getInt("m_role"));
				user.setDate(rs.getDate("m_date"));
			}
		} catch (SQLException e) {
			return null;
		}
		return user;
	}
	public UserDTO getUser(int id) {
		UserDTO user = null;
		Connection con = Controller.getConnection();
		String query = "select * from m_user where m_id = ?";
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()) {
				user = new UserDTO();
				user.setId(rs.getInt("m_id"));
				user.setUsername(rs.getString("m_username"));
				user.setPassword(rs.getString("m_password"));
				user.setRole(rs.getInt("m_role"));
				user.setDate(rs.getDate("m_date"));
			}
		} catch (SQLException e) {
			return null;
		}
		return user;
	}
	public UserDTO getUser(String username, String password) {
		UserDTO user = null;
		Connection con = Controller.getConnection();
		String query = "select * from m_user where m_username = ? and m_password = ?";
		PreparedStatement stmt;
		try {
			stmt = con.prepareStatement(query);
			stmt.setString(1, username);
			stmt.setString(2, password);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()) {
				user = new UserDTO();
				user.setId(rs.getInt("m_id"));
				user.setUsername(rs.getString("m_username"));
				user.setPassword(rs.getString("m_password"));
				user.setRole(rs.getInt("m_role"));
				user.setDate(rs.getDate("m_date"));
			}
		} catch (SQLException e) {
			return null;
		}
		return user;
	}
	public boolean deleteUser(String username) {
		String query = "delete from m_user where m_username = ?";
		Connection con = Controller.getConnection();
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setString(1, username);
			stmt.executeUpdate();
		} catch (SQLException e) {
			return false;
		}
		
		return true;
	}
	public boolean deleteUser(int id) {
		String query = "delete from m_user where m_id = ?";
		Connection con = Controller.getConnection();
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			stmt.executeUpdate();
		} catch (SQLException e) {
			return false;
		}
		
		return true;
	}
	public boolean addUser(String username, String password, int role) {
		String query = "insert into m_user(m_username, m_password, m_role, m_date) values (?, ?, ?, ?)";
		Connection con = Controller.getConnection();
		try {
			PreparedStatement stmt = con.prepareStatement(query);
			stmt.setString(1, username);
			stmt.setString(2, password);
			stmt.setInt(3, role);
			stmt.setDate(4, (java.sql.Date) new Date());
		} catch (SQLException e) {
			return false;
		}
		return true;
	}
	public boolean addUser(String username, String password) {
		String query = "insert into m_user(m_username, m_password, m_role, m_date) values(?, ?, ?, ?)";
		Connection con = Controller.getConnection();
		PreparedStatement stmt;
		try {
			stmt = con.prepareStatement(query);
			stmt.setString(1, username);
			stmt.setString(2, password);
			stmt.setInt(3, 0);
			stmt.setDate(4, (java.sql.Date) new Date());
		} catch (SQLException e) {
			return false;
		}
		return true;
	}
}
